package com.bj.dljd.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bj.dljd.constant.SystemConstant;
import com.bj.dljd.convent.UserPayConfigConvent;
import com.bj.dljd.domain.UserPayConfig;
import com.bj.dljd.domain.UserPayInfo;
import com.bj.dljd.models.query.UserConfigQuery;
import com.bj.dljd.models.response.R;
import com.bj.dljd.models.vo.UserPayInfoVo;
import com.bj.dljd.models.vo.UserPayVo;
import com.bj.dljd.service.UserConfigService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserProductConfigController {
    @Resource
    private UserPayConfigConvent userPayConfigConvent;

    @Resource
    private UserConfigService userConfigService;


    /*分页显示商户支付产品配置*/
    @GetMapping("/config/product/pageList")
    public R pageProductConfigList(@RequestParam Integer pageNo){
        pageNo = (pageNo == null || pageNo <1 ) ? 1 : pageNo;
        // 查询配置数据
        Page<UserPayConfig> page
                = userConfigService.productConfigList(pageNo, SystemConstant.PAGE_SIZE);

        long pages = page.getPages();
        List<UserPayConfig> records = page.getRecords();

        // 转换vo
        List<UserPayVo> userPayVos
                = userPayConfigConvent.convertListUserPayConfigVo(records);
        Map<String, Object> map = new HashMap<>();
        map.put("pages",pages);
        map.put("list",userPayVos);
        return R.Ok(map);
    }
    @PostMapping("/config/product/create")
    public R pageProductConfigList(
            @RequestBody UserConfigQuery configQuery){
        userConfigService.createUserConfig(configQuery);
        return R.Ok();
    }
    // 显示第三方支付配置
    @GetMapping("/config/payinfoList")
    public R pageThirdConfigList(
            @RequestParam String userNo, @RequestParam String productCode){
    if (StrUtil.isNotBlank(userNo) && StrUtil.isNotBlank(productCode)){
        List<UserPayInfo> payInfoList = userConfigService.payInfoList(userNo,productCode);
        List<UserPayInfoVo> userPayInfoVos = userPayConfigConvent.convertListUserPayVo(payInfoList);
        return R.Ok(userPayInfoVos);

    }
        return R.fuil("参数错误");
    }
    @PostMapping("/config/remove")
    public R removeConfig(
            @RequestParam String userNo, @RequestParam String productCode){
        if (StrUtil.isNotBlank(userNo) && StrUtil.isNotBlank(productCode)){
            userConfigService.removeConfig(userNo,productCode);
            return R.Ok();

        }
        return R.fuil("参数错误");
    }
}
